package ro.ubbcluj.cs.damate.persistence.entities;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import java.sql.Timestamp;
import java.util.List;

import static javax.persistence.CascadeType.*;
import static javax.persistence.FetchType.*;
import static javax.persistence.GenerationType.*;

/**
 * @author Lóránd Fazakas (lorand)
 *         Date: 4/22/12
 *         Time: 1:12 AM
 */
@Table(name = "event", catalog = "damate")
@Entity
public class Event {

    private Long eventId;

    @Column(name = "eventId", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
    @Id
    @GeneratedValue(strategy= IDENTITY)
    public Long getEventId() {
        return eventId;
    }

    public void setEventId(Long eventId) {
        this.eventId = eventId;
    }

    private Timestamp date;

    @Column(name = "date", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
    @Basic
    public Timestamp getDate() {
        return date;
    }

    public void setDate(Timestamp date) {
        this.date = date;
    }

    private EventType eventType;

    @ManyToOne(fetch = LAZY, cascade = {PERSIST, MERGE, REFRESH})
    @JoinColumn(name = "eventType")
    public EventType getEventType() {
        return eventType;
    }

    public void setEventType(EventType eventType) {
        this.eventType = eventType;
    }

    private List<EventParameter> parameters;

    @OneToMany(fetch = LAZY, cascade = ALL, mappedBy = "event")
    @OrderBy("paramName asc")
    public List<EventParameter> getParameters() {
        return parameters;
    }

    public void setParameters(List<EventParameter> parameters) {
        this.parameters = parameters;
    }

    private User relatedUser;

    @ManyToOne(fetch = LAZY, cascade = REFRESH)
    @JoinColumn(name = "user")
    public User getRelatedUser() {
        return relatedUser;
    }

    public void setRelatedUser(User relatedUser) {
        this.relatedUser = relatedUser;
    }

    private ObservedObject relatedObject;

    @ManyToOne(fetch = LAZY, cascade = {PERSIST, MERGE, REFRESH})
    @JoinColumn(name = "observedEntity")
    public ObservedObject getRelatedObject() {
        return relatedObject;
    }

    public void setRelatedObject(ObservedObject relatedObject) {
        this.relatedObject = relatedObject;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Event that = (Event) o;

        if (eventId != that.eventId) return false;
        if (date != null ? !date.equals(that.date) : that.date != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = eventId.intValue();
        result = 31 * result + (date != null ? date.hashCode() : 0);
        return result;
    }
}
